home *** CD-ROM | disk | FTP | other *** search
/ Gigarom 1 / Gigarom Macintosh Archives (Quantum Leap)(CDRM1080320)(1993).iso / FILES / DEV / I-Z / TransSkel.cpt / FakeAlert.pas < prev    next >
Pascal/Delphi Source File  |  1987-01-08  |  5KB  |  173 lines

  1. {    In-memory item list for dialog with four items:}
  2.  
  3. {    1    "^0^1^2^3" (static text)}
  4. {    2    Button 1}
  5. {    3    Button 2}
  6. {    4    Button 3}
  7.  
  8. {    The caller of FakeAlert passes the four strings that are to be}
  9. {    substituted into the first item, the number of buttons that}
  10. {    should be used, and the titles to put into each button.}
  11. {    A copy of the item list is hacked to use the right number of}
  12. {    buttons.}
  13.  
  14. {    Thanks to Erik Kilk and Jason Haines.  Some of the stuff to do}
  15. {    this is modified from code they wrote.}
  16.  
  17. {    Ported to LightSpeed Pascal 8 January 1987 Owen Hartnett    }
  18.  
  19. UNIT FakeAlert;
  20.  
  21. INTERFACE
  22.  
  23.     FUNCTION FakeAlert (s1, s2, s3, s4 : Str255;
  24.                                     nButtons, defButton : integer;
  25.                                     t1, t2, t3 : Str255) : integer;
  26. IMPLEMENTATION
  27.  
  28.     VAR
  29.         ItemList : ARRAY[0..32] OF integer;
  30.  
  31.         savePort : GrafPtr;
  32.         theDialog : DialogPtr;
  33.         iListHandle : Handle;
  34.         bounds : Rect;
  35.         itemHit : integer;
  36.  
  37.     PROCEDURE InitItemList;
  38.  
  39. {This proc performs static initializations on ItemList    }
  40.  
  41.     BEGIN
  42.         Itemlist[0] := 3;                    { max number of items - 1 }
  43.         Itemlist[1] := 0;                    {    statText item}
  44.                                         { reserve a long for item handle }
  45.         itemlist[2] := 0;                    { display rectangle }
  46.         Itemlist[3] := 10;
  47.         Itemlist[4] := 27;
  48.         itemlist[5] := 61;
  49.         Itemlist[6] := 225;
  50.         Itemlist[7] := $8808;    { 8 + 128 = statText (disabled), title 8 bytes long }
  51.         itemlist[8] := $5e30;    { ^0^1^2^3 }
  52.         Itemlist[9] := $5e31;
  53.         Itemlist[10] := $5e32;
  54.         itemlist[11] := $5e33;
  55. {    first button}
  56.         Itemlist[12] := 0;                { reserve a long for item handle }
  57.         Itemlist[13] := 0;
  58.         itemlist[14] := 104;                { display rectangle }
  59.         Itemlist[15] := 140;
  60.         Itemlist[16] := 124;
  61.         itemlist[17] := 210;
  62.         Itemlist[18] := $400;        { 4 = pushButton, title is 0 bytes long}
  63. {    second button}
  64.         Itemlist[19] := 0;                { reserve a long for item handle }
  65.         itemlist[20] := 0;
  66.         Itemlist[21] := 104;                { display rectangle }
  67.         Itemlist[22] := 30;
  68.         itemlist[23] := 124;
  69.         Itemlist[24] := 100;
  70.         Itemlist[25] := $400;        { 4 = pushButton, title is 0 bytes long}
  71. {    third button}
  72.         itemlist[26] := 0;                { reserve a long for item handle }
  73.         Itemlist[27] := 0;
  74.         Itemlist[28] := 72;                { display rectangle }
  75.         itemlist[29] := 30;
  76.         Itemlist[30] := 92;
  77.         Itemlist[31] := 100;
  78.         itemlist[32] := $400;        { 4 = pushButton, title is 0 bytes long}
  79.     END;
  80.  
  81. {    Set dialog button title and draw bold outline if makeBold true.}
  82. {    This must be done after the window is shown or else the bold}
  83. {    outline won't show up (which is probably the wrong way to do it).}
  84.  
  85.     PROCEDURE SetDControl (theDialog : DialogPtr;
  86.                                     itemNo : integer;
  87.                                     title : Str255;
  88.                                     makeBold : Boolean);
  89.         VAR
  90.             itemHandle : Handle;
  91.             itemType : integer;
  92.             itemRect : Rect;
  93.             pState : PenState;
  94.  
  95.     BEGIN
  96.         GetDItem(theDialog, itemNo, itemType, itemHandle, itemRect);
  97.         SetCTitle(ControlHandle(itemHandle), title);
  98.         IF makeBold THEN
  99.             BEGIN
  100.                 GetPenState(pState);
  101.                 PenNormal;
  102.                 PenSize(3, 3);
  103.                 InsetRect(itemRect, -4, -4);
  104.                 FrameRoundRect(itemRect, 16, 16);
  105.                 SetPenState(pState);
  106.             END;
  107.     END;
  108.  
  109. {    Fake an alert, using an in-memory window and item list.}
  110. {    The message to be presented is constructed from the first}
  111. {    four arguments.  nButtons is the number of buttons to use,}
  112. {    defButton is the default button, the next three args are}
  113. {    the titles to put into the buttons.  The return value is}
  114. {    the button number (1..nButtons).  This must be interpreted}
  115. {    by the caller, since the buttons may be given arbitrary}
  116. {    titles.}
  117.  
  118. {    nButtons should be between 1 and 3, inclusive.}
  119. {    defButton should be between 1 and nButtons, inclusive.}
  120.  
  121.     FUNCTION FakeAlert;
  122.         VAR
  123.             savePort : GrafPtr;
  124.             theDialog : DialogPtr;
  125.             iListHandle : Handle;
  126.             bounds : Rect;
  127.             itemHit : integer;
  128.  
  129.     BEGIN
  130.         InitItemList;
  131.         InitCursor;
  132.         GetPort(savePort);
  133.         iListHandle := NewHandle(longint(512));
  134.         HLock(iListHandle);
  135.         ItemList[0] := nButtons;                                { = number items - 1 }
  136.         BlockMove(@ItemList[0], iListHandle^, longint(512));
  137.         SetRect(bounds, 115, 80, 355, 220);
  138.         theDialog := NewDialog(NIL, bounds, '', false, dBoxProc, WindowPtr(-1), false, longint(0), iListHandle);
  139.         ParamText(s1, s2, s3, s4);                            { construct message }
  140.         SetPort(theDialog);
  141.         ShowWindow(theDialog);
  142.  
  143.         CASE nButtons OF                { set button titles }
  144.             3 : 
  145.                 BEGIN
  146.                     SetDControl(theDialog, 4, t3, defButton = 3);
  147.                     SetDControl(theDialog, 3, t2, defButton = 2);
  148.                     SetDControl(theDialog, 2, t1, defButton = 1);
  149.                 END;
  150.             2 : 
  151.                 BEGIN
  152.                     SetDControl(theDialog, 3, t2, defButton = 2);
  153.                     SetDControl(theDialog, 2, t1, defButton = 1);
  154.                 END;
  155.             1 : 
  156.                 SetDControl(theDialog, 2, t1, defButton = 1);
  157.         END;
  158.  
  159. {    ModalDialog returns 1 if return/enter hit, which, since}
  160. {    the statText item is first, can be unambiguously}
  161. {    interpreted as "choose default".}
  162.  
  163.         ModalDialog(NIL, itemHit);
  164.         IF itemHit = 1 THEN
  165.             itemHit := defButton
  166.         ELSE
  167.             itemHit := itemHit - 1;
  168.         HUnlock(iListHandle);
  169.         DisposDialog(theDialog);
  170.         SetPort(savePort);
  171.         FakeAlert := itemHit;
  172.     END;
  173. END.